// MainDlg.cpp : implementation of the CMainDlg class
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "resource.h"

#include "aboutdlg.h"
#include "configdlg.h"
#include "MainDlg.h"

BOOL CMainDlg::PreTranslateMessage(MSG* pMsg)
{
	return CWindow::IsDialogMessage(pMsg);
}

BOOL CMainDlg::OnIdle()
{
	return FALSE;
}

LRESULT CMainDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	ATL::CString strDevice;

	// center the dialog on the screen
	CenterWindow();

	// set icons
	HICON hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
		IMAGE_ICON, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR);
	SetIcon(hIcon, TRUE);
	HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
		IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
	SetIcon(hIconSmall, FALSE);


	// Attach UI
	m_ComboBox.Attach(GetDlgItem(IDC_COMBO1));

	// Initialize Default DCB
	// this may be updated by "Configure" dialog
	InitCommConfig();

	// List Devices COM1 - COM9 and COM0
	m_numdev = 0;
	for (int i = 1; i <= 10; i++) {

		strDevice.Format(TEXT("COM%d:"), (i % 10));
		if (TestDevice(strDevice.GetBuffer())) {
			// Test Open Success

			m_ComboBox.InsertString(m_numdev, strDevice.GetBuffer());

			ATL::CString *p = new ATL::CString(strDevice);	// copy object
			m_ComboBox.SetItemDataPtr(m_numdev, p);

			m_numdev++;
		}
	}

	if (m_numdev == 0) {
		m_ComboBox.InsertString(0, TEXT("(no device)"));
		m_ComboBox.SetItemDataPtr(0, NULL);
	}

	// anyway select 1st item
	m_ComboBox.SetCurSel(0);


	// register object for message filtering and idle updates
	CMessageLoop* pLoop = _Module.GetMessageLoop();
	ATLASSERT(pLoop != NULL);
	pLoop->AddMessageFilter(this);
	pLoop->AddIdleHandler(this);

	UIAddChildWindowContainer(m_hWnd);

	return TRUE;
}

LRESULT CMainDlg::OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CAboutDlg dlg;
	dlg.DoModal();
	return 0;
}

LRESULT CMainDlg::OnStartTerminal(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	// Get Device Name
	ATL::CString *p = (ATL::CString *)m_ComboBox.GetItemDataPtr(m_ComboBox.GetCurSel());
	ATL::CString title;
	HANDLE hComDev;

	if (!p) {
		MessageBox(TEXT("No COM Device"), APP_NAME, (MB_OK|MB_ICONERROR));
	}
	else if (!OpenDevice(p->GetBuffer(), hComDev)) {
		MessageBox(TEXT("Open Comm Device Failed."), APP_NAME, (MB_OK|MB_ICONERROR));
	}
	// otherwise, device open SUCCESS!
	else if (!SetupComm(hComDev)) {
		CloseHandle(hComDev);
		MessageBox(TEXT("Configure Comm Device Failed."), APP_NAME, (MB_OK|MB_ICONERROR));
	}
	else {
		// Now start terminal UI

		// Make DialogBox Hide
		ShowWindow(SW_HIDE);

		// This is Blocking operation
		title.Format(TEXT("%s - %s"), APP_NAME, p->GetBuffer());
		BOOL bRet = DisplayTerminalWindow(hComDev, title.GetBuffer());

		// anyway, close device
		CloseHandle(hComDev);

		// Restore DialogBox
		ShowWindow(SW_SHOWNORMAL);

		if (!bRet)		
		{
			MessageBox(TEXT("Display Terminal Window Failed."), APP_NAME, (MB_OK|MB_ICONERROR));
		}

	}

	return 0;
}

LRESULT CMainDlg::OnConfigDialog(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CConfigDlg dlg;
	dlg.DoModal();
	return 0;
}

LRESULT CMainDlg::OnOkCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CloseDialog(wID);
	return 0;
}

void CMainDlg::CloseDialog(int nVal)
{
	DestroyWindow();
	::PostQuitMessage(nVal);
}

